local file = "single.ctimer.lua"
local newc = require("newc")
local weak = reload("tools.weak")
local other = require("other")
---@class ctimer
local this = newc(file, "this")
local fmap = newc(file, "fmap", weak)
function this.cook()
    local cookid = this.cookid or 0
    this.cookid = cookid + 1
    return cookid
end

function this.callback()
    local cid = this.cook()
    other.timeout(cid, 100)
end

function this.launch()
    if this.start then
        return
    end
    this.callback()
    this.start = true
end

function this.register(that)
    fmap[that] = true
    this.launch()
end

function this.unregister(that)
    fmap[that] = nil
end

function this.tick(cid, nowms)
    this.callback()
    for that in pairs(fmap) do
        that:callback(nowms)
    end
end

return this
